//
//  TabBar.m
//  Weibo
//
//  Created by Anasue on 9/10/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "TabBar.h"

@interface TabBar ()
@property (nonatomic, weak) UIButton *plusButton;

@end

@implementation TabBar

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundImage = [UIImage imageWithName:@"tabbar_background"];
        self.selectionIndicatorImage = [UIImage imageWithName:@"navigationbar_button_background"];

        [self setupPlusButton];
    }
    return self;
}

- (void)setupPlusButton
{
    UIButton *plusButton = [[UIButton alloc]init];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];

    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:plusButton];
    self.plusButton = plusButton;
}

- (void)plusClick
{
    //设置代理，通知tabBar控制器弹出一个控制器
    if ([self.tabBardelegate respondsToSelector:@selector(tabBarDidClickedPlusButton:)]) {
        [self.tabBardelegate tabBarDidClickedPlusButton:self];
    }
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setupPlusButtonFrame];
    [self setupAllTabBarButtonFrame];
    
        
}

- (void)setupPlusButtonFrame
{
    self.plusButton.size = self.plusButton.currentBackgroundImage.size;
    self.plusButton.center = CGPointMake(self.size.width * 0.5, self.size.height * 0.5);
}

- (void)setupAllTabBarButtonFrame
{
    int index = 0;
    /**
     *  遍历tabBar上面的子控件
     */
    for (UIView *tabBarButton in self.subviews) {
        if (![tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        [self setupTabBarButtonFrame:tabBarButton atIndex:index];
//        [self setupTabBarButtonTextColor:tabBarButton atIndex:index];
        index++;
    }
}

- (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index
{
    CGFloat buttonW = self.width / (self.items.count + 1);
    CGFloat buttonH = self.height;
    tabBarButton.width = buttonW;
    tabBarButton.height = buttonH;
    tabBarButton.y = 0;
    if (index >= 2) {
        tabBarButton.x = buttonW * (index + 1);
    }else{
        tabBarButton.x = buttonW * index;
    }
}


/**
 *  该方法取消，在设置tabBar的属性时直接设置更简洁
 *
 */
/*
- (void)setupTabBarButtonTextColor:(UIView *)tabBarButton atIndex:(int)index
{
    int selectedIndex = [self.items indexOfObject:self.selectedItem];
    
    for (UILabel *label in tabBarButton.subviews) {
        
    if (![label isKindOfClass:[UILabel class]]) continue;
    label.font = [UIFont systemFontOfSize:10];
    if (selectedIndex == index) {
        label.textColor = [UIColor orangeColor];
    }else{
//        Log(@"%d",selectedIndex);
        label.textColor = [UIColor blackColor];
    }
}
}
 */




@end
